<template>
  <div class="execution">
    <basic-container>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="未缴纳" name="first"></el-tab-pane>
        <el-tab-pane label="已缴纳" name="second"></el-tab-pane>
      </el-tabs>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :search.sync="searchForm"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @search-reset="resetChange"
        @selection-change="selectionChange"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            v-if="activeName == 'first'"
            @click="handleAdd"
          >
            新建
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-notebook-2"
            v-if="activeName == 'first'"
            @click="pays('payment')"
          >
            缴费
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-tickets"
            v-if="activeName == 'second'"
            @click="pays('invoice')"
          >
            开票
          </el-button>
        </template>
        <template slot="costType" slot-scope="scope">
          {{ scope.row.costType | getCostType(that) }}
        </template>
        <template slot="costTypeSearch">
          <el-select
            v-model="searchForm.costType"
            clearable
            placeholder="选择费用类型"
          >
            <el-option
              v-for="(item, index) in costTypeOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </template>

        <template slot="searchMenu">
          <el-button size="small" icon="el-icon-download" @click="exportExcel"
            >导出</el-button
          >
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            :type="'text'"
            v-if="scope.row.isPayment == 0"
            @click="pay(scope.row,'payment')"
            >缴费</el-button
          >
          <el-button
            :type="'text'"
            v-if="scope.row.isPayment == 1"
            @click="pay(scope.row,'invoice')"
            >开票</el-button
          >
          <el-button :type="'text'" @click="detail(scope.row)">查看</el-button>
        </template>
      </avue-crud>
      <pay ref="pay" @refreshList="refreshList"></pay>
      <detail ref="detail"></detail>
      <addForm ref="addForm"></addForm>
    </basic-container>
  </div>
</template>

<script>
import { fetchList } from "@/api/cost/paymentscost";
import { tableOption } from "@/const/crud/cost/paymentscost";
import { mapGetters } from "vuex";
import { getDicType, ExportFile } from "@/api/common";
import pay from "./pay";
import detail from "./detail";
import addForm from "./formModify";
export default {
  name: "mrcost",
  components: { pay, detail, addForm },
  data() {
    return {
      activeName: "first",
      searchForm: {
        isPayment: 0,
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      costTypeOptions: [],
      that: this,
      selectList: [],
      formVisible: false,
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.generator_mrcost_add, false),
        delBtn: this.vaildData(this.permissions.generator_mrcost_del, false),
        editBtn: this.vaildData(this.permissions.generator_mrcost_edit, false),
      };
    },
  },
  mounted() {
    getDicType("cost_type").then((res) => {
      this.costTypeOptions = res.data.data;
    });
  },
  filters: {
    getCostType(value, that) {
      let str = "";
      that.costTypeOptions.forEach((item) => {
        if (value == item.value) {
          str = item.label;
        }
      });
      return str;
    },
  },
  methods: {
    exportExcel() {
      ExportFile("/cost/park/paymentscost/export", this.searchForm);
    },
    // 切换状态
    handleClick(tab, event) {
      if (this.activeName == "first") {
        this.searchForm.isPayment = 0;
      } else {
        this.searchForm.isPayment = 1;
      }
      this.getList();
    },
    detail(row) {
      this.$refs.detail.init(row);
    },
    pay(item,action) {
      this.$refs.pay.action = action;
      this.$refs.pay.init([item], 2);
    },
    refreshList() {
      this.getList();
    },
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          params,
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    searchChange(form, done) {
      // this.searchForm = form
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    refreshChange() {
      this.getList(this.page);
    },
    resetChange() {
      this.searchForm.costType = "";
      this.searchForm.month = "";
    },
    selectionChange(list) {
      this.selectList = [];
      this.selectList = list;
    },
    isIdentical(type) {
      let flag = true;
      let idList = [];
      this.selectList.some((item, index) => {
        if (idList.includes(item.companyId)) {
        } else {
          idList.push(item.companyId);
        }
        if (idList.length >= 2) {
          flag = false;
          return true;
        }
      });
      if (!flag) {
        let name = type ? "缴费" : "开票";
        let title = type ? "缴费提示" : "开票提示";
        this.$alert(
          "<div>当前选中的公司不一致，无法进行" + name + "操作</div>",
          title,
          {
            dangerouslyUseHTMLString: true,
          }
        );
      }
      return flag;
    },
    pays(action) {
      if (this.selectList.length >= 2 && !this.isIdentical(1)) {
        return;
      }
      let IsPay = true;
      let indexList = [];
      this.selectList.forEach((item, index) => {
        if (item.isPayment == 1) {
          IsPay = false;
          indexList.push(item.$index + 1);
        }
      });
      if (IsPay) {
        this.$refs.pay.action = action;
        this.$refs.pay.init(this.selectList, 2);
      } else {
        this.$alert(
          "<div>以下数据序号存在已缴费</div><div>序号分别为<span style='font-size:18px;font-weight: bold;'>[" +
            indexList +
            "]</span></div>",
          "缴费提示",
          {
            dangerouslyUseHTMLString: true,
          }
        );
      }
    },
    handleAdd() {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.addForm.init("add");
      });
    },
  },
};
</script>
